<?php

namespace App\Http\Controllers\Api\O1\UserCenter\Order;

use Illuminate\Http\Request;
use Biz\Services\Site\SiteOrderService;
use Biz\Transformers\Site\OrderTransformer;
use Lumen\Core\Exceptions\BusinessException;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * OrderController.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class OrderController extends BaseController
{
    /**
     * @var mixed
     */
    protected $service;

    /**
     * OrderController constructor.
     *
     * @param SiteOrderService $service
     */
    public function __construct(SiteOrderService $service)
    {
        $this->service = $service;
    }

    /**
     * 取消订单.
     *
     * @param string $id
     *
     * @return \Illuminate\Http\Response
     */
    public function cancel(string $id)
    {
        $order = $this->service->cancel($id);

        return new OrderTransformer($order);
    }

    /**
     * 订单付款中.
     *
     * @param Request $request
     * @param string  $order_no
     *
     * @throws BusinessException
     *
     * @return \Illuminate\Http\Response
     */
    public function paying(Request $request, string $order_no)
    {
        // 获取订单
        $order     = $this->service->findByOrderNo($order_no);
        $orderUser = $order->user;

        // 检测订单所有者
        $user = $request->user();
        if ($user->id !== $orderUser->id) {
            throw new BusinessException(trans('site/order/order.not_belong_user'));
        }

        // 检测站点
        $site_id = $request->get('site_id');
        if ($order->site_id !== $site_id) {
            throw new BusinessException(trans('site/order/order.not_belong_site'));
        }

        // 更新状态为付款中
        $order = $this->service->paying($order);

        return new OrderTransformer($order);
    }
}
